package cn.ss.exam2302.web;

import cn.ss.exam2302.entity.*;
import cn.ss.exam2302.service.PaperService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
public class PaperController {
    @Autowired
    private PaperService paperService;

    @GetMapping("/test")
    @RequiresRoles("USER")
    public String test(){
        return "test";
    }

    @PostMapping("/query")
    @RequiresRoles("USER")
    @ResponseBody
    public JsonResult query(Integer paId, String paPassword){
        return paperService.query(paId, paPassword);
    }

    @PostMapping("/hand")
    @RequiresRoles("USER")
    @ResponseBody
    public void hand(Integer paId){
        paperService.hand(paId);
    }

    @GetMapping("/score")
    @RequiresRoles("USER")
    public String score(Model model){
        List<Score> scoreList = paperService.score();
        model.addAttribute("scoreList", scoreList);
        return "score";
    }

    @GetMapping("/addPaper")
    @RequiresRoles("ADMIN")
    public String addPaper(){
        return "addPaper";
    }

    @PostMapping("/addPaper")
    @RequiresRoles("ADMIN")
    @ResponseBody
    public JsonResult add(Paper paper){
        return paperService.add(paper);
    }

    @GetMapping("/managePaper")
    @RequiresRoles("ADMIN")
    public String managePaper(Model model){
        List<Paper> paperList = paperService.queryAll();
        model.addAttribute("paperList", paperList);
        return "managePaper";
    }

    @PostMapping("/upload")
    @RequiresRoles("ADMIN")
    @ResponseBody
    public JsonResult upload(MultipartFile file, Integer paId) throws IOException {
        return paperService.upload(file, paId);
    }

    @PostMapping("/setStatus")
    @RequiresRoles("ADMIN")
    @ResponseBody
    public JsonResult setStatus(String status, Integer paId) {
        return paperService.setStatus(status, paId);
    }

    @PostMapping("/setPassword")
    @RequiresRoles("ADMIN")
    @ResponseBody
    public JsonResult setPassword(String password, Integer paId) {
        return paperService.setPassword(password, paId);
    }
}
